home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Documentation / develop / develop Issue 20 / develop 20 code / QTTextSample / Aevt.c next >
Encoding:
Text File  |  1994-10-14  |  8.5 KB  |  314 lines  |  [TEXT/KAHL]

  1. // Simple framework for Macintosh sample code
  2. //
  3. // Nick Thompson, DEVSUPPORT
  4. //
  5. // This file contains the appleevent related code code for the framework.
  6. // Alle we do in here is provide support for the required AppleEvents
  7. // 
  8. // 9/16/94    nick    first cut
  9. // 10/14/94    nick    cleaned up for release
  10.  
  11. #define    kGestaltTrap    0xA0AD
  12.  
  13. #include <GestaltEqu.h>
  14.  
  15. #include "Aevt.h"
  16. #include "MyTraps.h"
  17. #include "Mtb.h"
  18. #include "TextMediaExample.h"
  19.  
  20. // Global scope
  21. extern Boolean            gQuitFlag ;
  22.  
  23. // The have scope only in this file
  24.  
  25. static AEAddressDesc        pSelfAddress;        // A self-addressed address descriptor record
  26. static ProcessSerialNumber    pSelfPSN;            // This application's psn
  27. static AEDesc                pNullDesc;            // A null descriptor record
  28.  
  29. //-----------------------------------------------------------------------
  30. // make sure you call this to init the things used in this file
  31.  
  32. void InitAEStuff( void ) 
  33. {
  34.     // Set up the self-addressed descriptor record.
  35.     pSelfPSN.highLongOfPSN = 0;
  36.     pSelfPSN.lowLongOfPSN = kCurrentProcess;        // Use this instead of GetCurrentProcess
  37.                                                     // to allow appleevents to ourself
  38.                                                     // to be directly dispatched ( a
  39.                                                     // subroutine call to our handler )
  40.                                                     // instead of having to be processed 
  41.                                                     // through our event loop as events 
  42.                                                     // coming in from the outside will be.
  43.  
  44.     CheckError(AECreateDesc( typeProcessSerialNumber,
  45.                              (Ptr)&pSelfPSN,
  46.                              sizeof(ProcessSerialNumber),
  47.                              &pSelfAddress),
  48.                "\pCouldn't create a PSN descriptor for pSelfAddress");
  49.  
  50.     pNullDesc.descriptorType = typeNull;            // Initialize the global null descriptor record.
  51.     pNullDesc.dataHandle = nil;
  52.  
  53. }
  54.  
  55.  
  56. //-----------------------------------------------------------------------
  57. // returns true if the platform supports appleevents - we won't run
  58. // if it doesn't
  59.  
  60. Boolean SupportsAEVT(void)
  61. {
  62.     OSErr err;
  63.     long response;
  64.     
  65.     if (!TrapAvailable(kGestaltTrap))
  66.         return false;
  67.     
  68.     err = Gestalt(gestaltAppleEventsAttr,&response);
  69.     if (err!=noErr)
  70.         return false;
  71.         
  72.     return (response && (response << gestaltAppleEventsPresent));
  73. }
  74.  
  75. //-----------------------------------------------------------------------
  76. // called to process high level appleevents
  77.  
  78. void DoHighLevelEvent(EventRecord *ev)
  79. {
  80.     OSErr err;
  81.     
  82.     err = AEProcessAppleEvent(ev);    
  83. }
  84.  
  85.  
  86. //-----------------------------------------------------------------------
  87. // called to register our appleevent handlers
  88.  
  89. void RegisterMyEvents(void)
  90. {
  91.     OSErr err;
  92.     
  93.     if (!SupportsAEVT())
  94.         return;
  95.     
  96.     err = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(MyAEHandleOAPP),0L,false);
  97.     if (err!=noErr)
  98.         return;
  99.                 
  100.     err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(MyAEHandleODOC),0L,false);
  101.     if (err!=noErr)
  102.         return;
  103.                 
  104.     err = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(MyAEHandlePDOC),0L,false);
  105.     if (err!=noErr)
  106.         return;
  107.                 
  108.     err = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(MyAEHandleQUIT),0L,false);
  109.     if (err!=noErr)
  110.         return;            
  111. }
  112.  
  113. //-----------------------------------------------------------------------
  114. // open application event handler for the core event suite
  115.  
  116. pascal OSErr MyAEHandleOAPP(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  117. {
  118.     OSErr err = noErr;
  119.  
  120.     return err;
  121. }
  122.  
  123.  
  124. //-----------------------------------------------------------------------
  125. // handler for the open document appleevent handler
  126.  
  127. pascal OSErr MyAEHandleODOC(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  128. {
  129.     FSSpec         myFSS;
  130.     AEDescList    docList;
  131.     OSErr        err,
  132.                 ignoreErr;
  133.     long        index,
  134.                 itemsInList;
  135.     Size         actualSize;
  136.     AEKeyword    keywd;
  137.     DescType    returnedType;
  138.     Size        size;
  139.     
  140.  
  141.     err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList);
  142.     if (err == noErr) {
  143.     
  144.         // see how many descriptor items are in the list
  145.         // this is the number of documents we want to open
  146.         err = AECountItems(&docList,&itemsInList);
  147.  
  148.         // now get each descriptor record from the list
  149.         // coerce the returned data to an FSSpec record, and
  150.         // open the asoociated file
  151.         
  152.         for (index=1; index <= itemsInList && err == noErr; index++) {
  153.         
  154.             err = AEGetNthPtr(    &docList, 
  155.                                 index,
  156.                                 typeFSS,
  157.                                 &keywd,
  158.                                 &returnedType,
  159.                                 (Ptr)&myFSS,
  160.                                 sizeof(myFSS),
  161.                                 &actualSize);
  162.     
  163.             if (err == noErr)    {                    
  164.                 HandleOpenDoc( &myFSS ) ;
  165.             }
  166.         }
  167.         ignoreErr = AEDisposeDesc(&docList);
  168.     }
  169.     return err ;
  170. }
  171.  
  172. //-----------------------------------------------------------------------
  173. // handler for the print document event handler
  174.  
  175. pascal OSErr MyAEHandlePDOC(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  176. {
  177.     FSSpec         myFSS;
  178.     AEDescList    docList;
  179.     OSErr        err;
  180.     long        index,
  181.                 itemsInList;
  182.     Size         actualSize;
  183.     AEKeyword    keywd;
  184.     DescType    returnedType;
  185.     Size        size;
  186.     
  187.     err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList);
  188.     if (err == noErr) {
  189.     
  190.         // see how many descriptor items are in the list
  191.         // this is the number of documents we want to open
  192.         err = AECountItems(&docList,&itemsInList);
  193.  
  194.         // now get each descriptor record from the list
  195.         // coerce the returned data to an FSSpec record, and
  196.         // open the asoociated file
  197.         
  198.         for (index=1; index <= itemsInList && err == noErr; index++) {
  199.         
  200.             err = AEGetNthPtr(    &docList, 
  201.                                 index,
  202.                                 typeFSS,
  203.                                 &keywd,
  204.                                 &returnedType,
  205.                                 (Ptr)&myFSS,
  206.                                 sizeof(myFSS),
  207.                                 &actualSize);
  208.     
  209.             if (err == noErr)    {                    
  210.                 err = HandlePrintDoc( &myFSS );
  211.             }
  212.         }
  213.         err = AEDisposeDesc(&docList);
  214.     }
  215.     return err ;
  216. }
  217.  
  218. //-----------------------------------------------------------------------
  219. // quit appleevent handler
  220.  
  221. pascal OSErr MyAEHandleQUIT(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  222. {
  223.     OSErr         err = noErr;
  224.     
  225.     // close all windows and signal to Quit
  226.     WindowPeek    theWindow = nil;
  227.     
  228.     for( theWindow = (WindowPeek)FrontWindow(); theWindow != nil; theWindow = theWindow->nextWindow) {
  229.         DoDestroyMovieWindow((WindowPtr)theWindow) ;
  230.     }
  231.  
  232.       err = AEDisposeDesc(&pSelfAddress);            // Dispose of my self-addressed descriptor.
  233.     
  234.     gQuitFlag = true;
  235.     return err ;
  236. }
  237.  
  238. //----------------------------------------------------------------------------------//
  239. //    Send a Quit Application Apple Event to myself to terminate this app.        
  240.  
  241. void SendQuitApp( void )
  242. {
  243.     AppleEvent    myAppleEvent, reply;
  244.     
  245.     //    Create the Apple Event.
  246.     CheckError(AECreateAppleEvent( kCoreEventClass, 
  247.                                   kAEQuitApplication, 
  248.                                   &pSelfAddress,
  249.                                   kAutoGenerateReturnID, 
  250.                                   kAnyTransactionID, 
  251.                                   &myAppleEvent), "\pCouldn't create a 'quit' AppleEvent");
  252.                                   
  253.     //    Send the Apple Event.
  254.       CheckError(AESend( &myAppleEvent, 
  255.                         &reply, 
  256.                         kAENoReply+kAENeverInteract, 
  257.                         kAENormalPriority,
  258.                         kAEDefaultTimeout, 
  259.                         nil, 
  260.                         nil), "\pCouldn't send a 'quit' AppleEvent");
  261.                         
  262.       AEDisposeDesc(&myAppleEvent);                // Dispose of the Apple Event.
  263. } // SendQuitApp
  264.  
  265. //----------------------------------------------------------------------------------//
  266. //    Send a Open Document Application Apple Event to myself to open a document.        
  267.  
  268. void SendOpenDoc(FSSpec *myFSSpec)
  269. {
  270.      AppleEvent    myAppleEvent;
  271.     AppleEvent    defReply;
  272.     AEDescList    docList;
  273.     OSErr         myErr;
  274.     OSErr         ignoreErr;
  275.     
  276.     myAppleEvent.dataHandle = nil;
  277.     docList.dataHandle  = nil;
  278.     defReply.dataHandle = nil;
  279.         
  280.     // Create empty list and add one file spec
  281.     CheckError(AECreateList(nil,0,false, &docList), "\pCouldn't create a list descriptor for an 'odoc' AppleEvent");
  282.     
  283.     CheckError(AEPutPtr(&docList,1,typeFSS,(Ptr)myFSSpec,sizeof(FSSpec)), "\pCouldn't insert the FSSpec for an 'odoc' AppleEvent");
  284.         
  285.     CheckError(AECreateAppleEvent(    kCoreEventClass,
  286.                                     kAEOpenDocuments,
  287.                                     &pSelfAddress,
  288.                                     kAutoGenerateReturnID,
  289.                                     kAnyTransactionID,
  290.                                     &myAppleEvent), "\pCouldn't create an 'odoc' AppleEvent");
  291.  
  292.     // Put Params into our event and send it
  293.  
  294.     CheckError(AEPutParamDesc( &myAppleEvent,
  295.                               keyDirectObject,
  296.                               &docList), "\pCouldn't package FSSpec for an 'odoc' AppleEvent");
  297.  
  298.     CheckError(AESend( &myAppleEvent,
  299.                       &defReply,
  300.                       kAENoReply+kAENeverInteract,
  301.                       kAENormalPriority,
  302.                       kAEDefaultTimeout,
  303.                       nil,
  304.                       nil), "\pCouldn't send an 'odoc' AppleEvent");
  305.         
  306.         
  307.     if (myAppleEvent.dataHandle) 
  308.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  309.         
  310.     if (docList.dataHandle) 
  311.         ignoreErr = AEDisposeDesc(&docList);
  312.             
  313. }    // SendOpenDoc 
  314.